<template>   

    <Form ref="formItem" :model="formItem" :label-width="80" :rules="ruleValidate">
        <FormItem label="标题" prop="name">
            <Input v-model="formItem.name" placeholder="标题"></Input>
        </FormItem>
        <FormItem label="类型" prop="type">
            <Select v-model="formItem.type">
                <Option v-for="item in typeList" :value="item.id" :key="item.id"> {{item.lable}}</Option>            
            </Select>
        </FormItem>
        <FormItem label="开始时间" prop="specificdate">
            <DatePicker v-model="formItem.specificdate" type="date" placeholder="开始时间"></DatePicker>
        </FormItem>

        <FormItem v-if="infoId==false" label="天数">
            <InputNumber :min="1" v-model="days"></InputNumber>
        </FormItem> 
        <FormItem>
            <Button type="primary" @click="submit('formItem')">保存</Button>
        </FormItem>
        <div style="clear:both"></div>
        <Spin size="large" fix v-if="spinShow"></Spin>
    </Form>
</template>

<script>
export default {
    props: {
        infoId: {
            type: [Boolean,Number],
            default: false
        }
    },
    
    data () {
        return {
            typeList:[
                {
                    id:1,
                    lable:'休息日'
                },
                {
                    id:2,
                    lable:'法定节假日'
                },
                {
                    id:3,
                    lable:'放假'
                }
            ],

            days:1,

            infoUrl:`api/teacher/v1/certificaholidays/find/${this.infoId}`,//请求单条数据
            saveUrl:'',//保存接口
            saveType:null,//保存方式

            spinShow:false,
            infoOld:[],//请求的数据

            formItem: {      
                specificdate:'',
                type:1,
                name:null,//标题
                days:0,
            },
            ruleValidate: {
                specificdate: [
                    { required: true, type: 'date', message: '开始时间不得为空', trigger: 'change' }
                ],
                name: [
                    { required: true, message: '标题不得为空', trigger: 'blur' }
                ],
            }
            
          
        }
    },
    methods: {
        /************************************************************
         * 
         * 提交时
         * 
         ************************************************************/
        submit (name) {
            this.$refs[name].validate((valid) => {
                if (valid) {
                    this.spinShow=true//输入框禁用   
                    if(this.infoId==false){
                        this.formItem.days=this.days-1
                    }
                    this.saveType(this.saveUrl,this.formItem).then(val=>{
                        this.$emit('showParent',false)
                        this.$emit('backList',true)
                        this.$Message.success(val.message);
                        this.spinShow=false
                    }).catch(err => {
                        if (err.code && err.code == 422) {
                            this.$Message.error(err.message);
                        }
                        this.spinShow=false
                    }) 
                } else {
                    this.$Message.error('验证失败');
                }
            })
            
        },
        
        /************************************************************
         * 
         * 单条数据请求
         * 
         ************************************************************/
        infoData(){
            this.spinShow=true//输入框禁用
            this.$http.get(this.infoUrl).then(val=>{ 
                this.infoOld = JSON.parse(JSON.stringify(val.data))
                this.formItem.specificdate = this.infoOld.specificdate
                this.formItem.type = parseInt(this.infoOld.type)
                this.formItem.name = this.infoOld.name
                this.spinShow=false//输入框禁用 
            }).catch(err => {
                console.log(err);
            })
        },
        
        
    },
    mounted(){
        if(this.infoId==false){
            // add
            this.UpImgShow=true
            this.saveUrl='api/teacher/v1/certificaholidays/add'
            this.saveType=this.$http.post

        }else{
            // edit
            this.infoData();
            this.saveUrl=`api/teacher/v1/certificaholidays/edit/${this.infoId}`
            this.saveType=this.$http.put
        }
    },
}
</script>
